const Base = require('./base.js');
const md5 = require('md5');
const redis = require('redis');

module.exports = class extends Base {
    async listAction() {
        let app_key = this.get().app_key;
        let whereMap = {};
        if (!think.isEmpty(app_key)) {
            whereMap.app_key = ['like', `%${app_key}%`];
        }
        let model = this.model('version_history');
        let result = await model.where(whereMap).page(this.get('page'), this.get('limit')).countSelect();
        return this.jsonPage(result);
    }

    async fileVersionListAction() {
        let active = this.get().active;
        let app_key = this.get().app_key;
        let whereMap = {};
        if (!think.isEmpty(app_key)) {
            whereMap.app_key = ['like', `%${app_key}%`];
        }
        if (!think.isEmpty(active)) {
            whereMap.active = ['like', `%${active}%`];
        }
        let model = this.model('file_version');
        let result = await model.where(whereMap).page(this.get('page'), this.get('limit')).countSelect();
        return this.jsonPage(result);
    }

    async getApplicationAction() {
        let data = await this.model('application').select();
        return this.json(data);
    }

    async getConfigAction() {
        let version_history = this.model('version_history');
        let history = await version_history.where({id: this.post().id}).find();
        let config_md5 = history.config_md5;
        let base_line_md5 = history.base_line_md5;
        let featrue_md5 = history.featrue_md5;
        let file = this.model('file');
        let whereMap = {};
        let resultMap = {};
        whereMap.con_md5 = config_md5;
        let data = await file.where(whereMap).find();
        if (typeof (data.contents) != 'undefined') {
            resultMap.config_md5 = data.contents;
        } else {
            resultMap.config_md5 = '';
        }
        whereMap.con_md5 = base_line_md5;
        data = await file.where(whereMap).find();
        if (typeof (data.contents) != 'undefined') {
            resultMap.base_line_md5 = data.contents;
        } else {
            resultMap.base_line_md5 = '';
        }
        whereMap.con_md5 = featrue_md5;
        data = await file.where(whereMap).find();
        if (typeof (data.contents) != 'undefined') {
            resultMap.featrue_md5 = data.contents;
        } else {
            resultMap.featrue_md5 = '';
        }
        resultMap.id = this.post().id;
        return this.json(resultMap);
    }


    async getVersionConfigAction() {
        let db = this.model('file_version');
        let file_version = await db.where({id: this.post().id}).find();
        let config_md5 = file_version.config_md5;
        let base_line_md5 = file_version.base_line_md5;
        let featrue_md5 = file_version.featrue_md5;
        let file = this.model('file');
        let whereMap = {};
        let resultMap = {};
        whereMap.con_md5 = config_md5;
        let data = await file.where(whereMap).find();
        if (typeof (data.contents) != 'undefined') {
            resultMap.config_md5 = data.contents;
        } else {
            resultMap.config_md5 = '';
        }
        whereMap.con_md5 = base_line_md5;
        data = await file.where(whereMap).find();
        if (typeof (data.contents) != 'undefined') {
            resultMap.base_line_md5 = data.contents;
        } else {
            resultMap.base_line_md5 = '';
        }
        whereMap.con_md5 = featrue_md5;
        data = await file.where(whereMap).find();
        if (typeof (data.contents) != 'undefined') {
            resultMap.featrue_md5 = data.contents;
        } else {
            resultMap.featrue_md5 = '';
        }
        resultMap.id = this.post().id;
        return this.json(resultMap);
    }

    // async updateAction() {
    //     let data = this.post();
    //     let file = this.model('file');
    //     let version_history = this.model('version_history');
    //     let history = await version_history.where({id:data.id}).find();      //找到原始的数据
    //     //获得原始的
    //     let select_config_md5 = history.config_md5;
    //     let select_base_line_md5 = history.base_line_md5;
    //     let select_featrue_md5 = history.featrue_md5;
    //     //获得上传的数据
    //     let config_md5 = data.config_md5;
    //     let base_line_md5 = data.base_line_md5;
    //     let featrue_md5 = data.featrue_md5;
    //
    //     let success = await file.where({con_md5: select_config_md5}).update({contents: config_md5});
    //     if (success <= 0) {
    //         let count = await file.add({contents: config_md5, con_md5: select_config_md5});
    //         if (count < 0) {
    //             return this.fail('0', '添加失败')
    //         }
    //     }
    //     success = await file.where({con_md5: select_base_line_md5}).update({contents: base_line_md5});
    //     if (success <= 0) {
    //         let count = await file.add({contents: base_line_md5, con_md5: select_base_line_md5});
    //         if (count < 0) {
    //             return this.fail('0', '添加失败')
    //         }
    //     }
    //     success = await file.where({con_md5: select_featrue_md5}).update({contents: featrue_md5});
    //     if (success <= 0) {
    //         let count = await file.add({contents: featrue_md5, con_md5: select_featrue_md5});
    //         if (count < 0) {
    //             return this.fail('0', '添加失败')
    //         }
    //     }
    //     return this.fail('1', '修改成功！');
    // }

    async enabledAction() {
        let data = this.post();
        let file = this.model('file_version');
        if (data.active == 0) {
            data.active = 1;
        } else {
            data.active = 0;
        }

        let result = await file.where({id: data.id}).update({active: data.active});
        if (result < 0) {
            return this.fail('0', '修改状态失败！');
        }
        return this.fail('1', '修改状态成功！');
    }

    async editPageAction() {
        return this.display();
    }

    // async fileUpdateAction() {
    //     let data = this.post();
    //     let file = this.model('file');
    //     let db = this.model('file_version');
    //     let file_version = await db.where({id:data.id}).find();      //找到原始的数据
    //     //获得原始的
    //     let select_config_md5 = file_version.config_md5;
    //     let select_base_line_md5 = file_version.base_line_md5;
    //     let select_featrue_md5 = file_version.featrue_md5;
    //     //获得上传的数据
    //     let config_md5 = data.config_md5;
    //     let base_line_md5 = data.base_line_md5;
    //     let featrue_md5 = data.featrue_md5;
    //
    //     let success = await file.where({con_md5: select_config_md5}).update({contents: config_md5});
    //     if (success <= 0) {
    //         let count = await file.add({contents: config_md5, con_md5: select_config_md5});
    //         if (count < 0) {
    //             return this.fail('0', '添加失败')
    //         }
    //     }
    //     success = await file.where({con_md5: select_base_line_md5}).update({contents: base_line_md5});
    //     if (success <= 0) {
    //         let count = await file.add({contents: base_line_md5, con_md5: select_base_line_md5});
    //         if (count < 0) {
    //             return this.fail('0', '添加失败')
    //         }
    //     }
    //     success = await file.where({con_md5: select_featrue_md5}).update({contents: featrue_md5});
    //     if (success <= 0) {
    //         let count = await file.add({contents: featrue_md5, con_md5: select_featrue_md5});
    //         if (count < 0) {
    //             return this.fail('0', '添加失败')
    //         }
    //     }
    //     return this.fail('1', '修改成功！');
    // }
}
;
